//@author: Olfa

function traceMap() {


// init the map and create a cluster with some markers
    $('#myMap').gmap3({

        map: {
            options: {
                /* center:[25.132318,80.32052349],*/
                center: [48.5144, 5.2103],
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                zoom: 5
            }

        }


    });


};

//zoomLevel=10 : if level Transaction
//zoomLevel=9 : if level SHOP
//zoomLevel=8 : if level VILLAGE
//zoomLevel=7 : if level DISTRICT/DIVISION
//zoomLevel=6 : if level REGION
//zoomLevel=5 : if level COUNTRY

function changeZoom(requestParams) {

    $('#myMap').gmap3("clear");

    if (compteur == 0) {
        traceMap();
    } else {
    }

    switch (requestParams) {
        case "1":


            $('#myMap').gmap3({

                map: {
                    options: {
                        zoom: 5
                    }
                }
            });


            break;
        case "2":


            $('#myMap').gmap3({

                map: {
                    options: {
                        zoom: 6
                    }
                }
            });

            break;

        case "3":

            $('#myMap').gmap3({

                map: {
                    options: {
                        zoom: 7
                    }
                }
            });

            break;
        case "4":

            $('#myMap').gmap3({

                map: {
                    options: {
                        zoom: 7
                    }
                }
            });

            break;
        case "5":

            $('#myMap').gmap3({

                map: {
                    options: {
                        zoom: 8
                    }
                }
            });

            break;
        case "6":
            if (compteur == 0) {
                traceMap();
            }
            $('#myMap').gmap3({

                map: {
                    options: {
                        zoom: 8
                    }
                }
            });

            break;
        case "7":

            $('#myMap').gmap3({

                map: {
                    options: {
                        zoom: 10
                    }
                }
            });

            break;
        default:

            break;
    }


    if (requestParams != "") {

        $.ajax({
            url: "/First-EndToEnd/trace/getAllTraces",
            minTimeout: 5000,
            success: function (data) {

                positions = [];
                if (data.status) {
                    maxTracesNew = data.tracesCount;
                    addMarker(data);
                } else {
                    positions = [];
                }
            }
        });


        maxTracesOld = maxTracesNew;
        $('#myMap').smartupdater(
            {
                minTimeout: 5000,
                timeout: 3000
            }, function () {
                if (maxTracesNew > maxTracesOld) {//there is a new traceInstance
                    alert("Loading Changes...");
                    $.ajax({
                        url: "http://localhost:8080/First-EndToEnd/trace/getAllTraces",
                        minTimeout: 5000,
                        success: function (data) {

                            positions = [];
                            if (data.status) {
                                maxTracesNew = data.tracesCount;
                                addMarker(data);
                            } else {
                                positions = [];
                            }
                        }
                    });
                } else {
                }
            }
        );

    } else {
        //there is no agent selected
    }

    compteur++;

};


function agentChanged(requestParams) {
    positions = [];

    if (compteur == 0) {
        traceMap();
    } else {

    }
    $('#myMap').gmap3("clear");


    if (requestParams != "") {
        $.ajax({
            url: "http://localhost:8080/First-EndToEnd/trace/filterByAgent",
            minTimeout: 5000,
            data: {
                id: requestParams
            },


            success: function (data) {
                positions = [];

                if (data.status) {//there is traces rendered
                    maxTracesAgentNew = data.tracesCount;
                    addMarker(data);


                } else {
                    positions = [];

                }
            }
        });
        if (maxTracesAgentNew > maxTracesAgentOld) {//there is a new traceInstance
            maxTracesAgentOld = maxTracesAgentNew;
            $('#myMap').smartupdater(
                {
                    minTimeout: 5000,
                    timeout: 3000
                }, function () {
                    if (maxTracesAgentNew > maxTracesAgentOld) {//there is a new traceInstance
                        alert("Loading changes...");
                        $.ajax({
                            url: "/First-EndToEnd/trace/filterByAgent",
                            minTimeout: 5000,
                            data: {
                                id: requestParams
                            },


                            success: function (data) {
                                positions = [];

                                if (data.status) {//there is traces rendered
                                    maxTracesAgentNew = data.tracesCount;
                                    addMarker(data);


                                } else {
                                    positions = [];

                                }
                            }
                        });
                    } else {

                    }
                });

        } else {
            //there is NO level selected
        }
        compteur++;

    }
};


function categoryChanged(requestParams) {
    positions = [];

    if (compteur == 0) {
        traceMap();
    } else {

    }
    $('#myMap').gmap3("clear");


    if (requestParams != "") {
        $.ajax({
            url: "http://localhost:8080/First-EndToEnd/trace/filterByCategory",
            minTimeout: 5000,
            data: {
                id: requestParams
            },


            success: function (data) {
                positions = [];

                if (data.status) {//there is traces rendered
                    maxTracesAgentNew = data.tracesCount;
                    addMarker(data);


                } else {
                    positions = [];

                }
            }
        });
        if (maxTracesAgentNew > maxTracesAgentOld) {//there is a new traceInstance
            maxTracesAgentOld = maxTracesAgentNew;
            $('#myMap').smartupdater(
                {
                    minTimeout: 5000,
                    timeout: 3000
                }, function () {
                    alert("Loading changes...");
                    $.ajax({
                        url: "http://localhost:8080/First-EndToEnd/trace/filterByCategory",
                        minTimeout: 5000,
                        data: {
                            id: requestParams
                        },


                        success: function (data) {
                            positions = [];

                            if (data.status) {//there is traces rendered
                                maxTracesAgentNew = data.tracesCount;
                                addMarker(data);


                            } else {
                                positions = [];

                            }
                        }
                    });
                });
        } else {

        }
    } else {
        //there is NO level selected
    }
    compteur++;

};


// Place markers in by click
function addMarker(dataReceived) {

    var traceInfos = [];
    var contenuInfoBulle = '<h6>' + traceInfos + '</h6>';
    $.each(dataReceived.traces, function () {
        positions.push({latLng: [this.latitude, this.longitude]});
        console.log(positions);
    });
    $('#myMap').gmap3({

        marker: {
            values: positions,

            cluster: {
                radius: 100,
                // This style will be used for clusters with more than 0 markers
                0: {
                    content: '<div class="cluster cluster-1">CLUSTER_COUNT</div>',
                    width: 53,
                    height: 52

                },
// This style will be used for clusters with more than 20 markers
                20: {
                    content: '<div class="cluster cluster-2">CLUSTER_COUNT</div>',
                    width: 56,
                    height: 55
                },
// This style will be used for clusters with more than 50 markers
                50: {
                    content: '<div class="cluster cluster-3">CLUSTER_COUNT</div>',
                    width: 66,
                    height: 65
                }
            },

            events: {
                mouseover: function (marker, event, context) {

                    $.each(dataReceived.traces, function () {

                        if (marker != null) {
                            var markerLat = Math.round(marker.getPosition().lat() * 100) / 100;
                            var markerLng = Math.round(marker.getPosition().lng() * 100) / 100;
                            var posLat = Math.round(this.latitude * 100) / 100;
                            var posLng = Math.round(this.longitude * 100) / 100;

                            if (posLat == markerLat && posLng == markerLng) {
                                console.log("--->HERE" + this.pathPhoto);
                                if (this.pathPhoto != null) {
                                    marker.info = new google.maps.InfoWindow({//TODO delete web-app from this.pathPhoto
                                        content: '<h6>' + "Operation done by Agent: " + this.agent.id + " on: " + this.dateOp + '</br></h6><img src="../traceInstance/exemple.png"/>'
                                    });
                                } else {
                                    marker.info = new google.maps.InfoWindow({
                                        content: '<h6>' + "Operation done by Agent: " + this.agent.id + " on: " + this.dateOp + '</br></h6>'
                                    });
                                }

                                google.maps.event.addListener(marker, 'click', function () {
                                    marker.info.open($('#myMap'), marker);
                                });

                                console.log("***" + marker.getPosition().lat());
                                myMapMarker = marker;

                            } else {
                                //DONT ADD INFO
                                console.log("no data for this marker!!!");
                            }
                        } else {
                            //MARKER NULL
                            console.log("marker nullll!!!");
                        }
                    });

                },
                mouseout: function (marker, event, context) {
                    if (marker.info != null) {
                        marker.info.close();

                    }
                }
            }


        }


    });
};











